<template>
  <div class="footer-box">
    <div class="total-num-box">
      共<span>{{ total }}</span
      >项数据
    </div>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      :layout="layout"
      :total="checkTotal"
      :pager-count="pageCount"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "TrFooter",
  props: {
    total: {
      type: [Number, String],
      default: 0,
    },
    requestName: {
      type: String,
      default: "",
    },
    value: {
      type: Object,
      default: () => {},
    },
    sizeWord: {
      type: String,
      default: "pageSize",
    },
    currentWord: {
      type: String,
      default: "pageNo",
    },
    isDeepSearch: {
      type: Boolean,
      default: false,
    },
    layout: {
      type: String,
      default: "sizes, prev, pager, next, jumper",
    },
    pageCount: {
      type: Number,
      default: 7,
    },
  },
  computed: {
    checkTotal() {
      return Number(this.total);
    },
  },
  data: () => ({
    currentPage: 1,
    pageSize: 10,
    pageSizes: [10, 20, 30, 50],
    cloneParams: {},
  }),
  watch: {
    value: {
      handler(newVal) {
        this.cloneParams = Object.assign({}, newVal);
      },
      deep: true,
    },
  },
  methods: {
    handleSizeChange(p) {
      this.currentPage = 1;
      this.pageSize = p;
      this.$set(this.cloneParams, this.currentWord, 1);
      this.$set(this.cloneParams, this.sizeWord, p);
      this.$emit("input", this.cloneParams);
      this.requestHandle();
    },
    handleCurrentChange(p) {
      this.currentPage = p;
      this.$set(this.cloneParams, this.currentWord, p);
      this.$emit("input", this.cloneParams);
      this.requestHandle();
    },
    requestHandle() {
      if (this.isDeepSearch) {
        let obj = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        };
        this.$nextTick(() => {
          this.$emit("footer-search", obj);
        });
        return;
      }
      this.$nextTick(() => {
        this.$parent[this.requestName]();
      });
    },
  },
  mounted() {
    this.cloneParams = this.value;
  },
};
</script>

<style lang="scss" scoped>
.footer-box {
  position: relative;
  padding: 16px 0;

  .total-num-box {
    position: absolute;
    left: 0;
    top: 24px;
    color: rgba(#000, 0.4);
    font-weight: 400;
    font-size: 14px;
  }

  span {
    padding: 0 4px;
  }
}
</style>
